package com.youku.plugins.commonui.view
{
   import flash.display.MovieClip;
   import com.youku.interfaces.IPlayerProxy;
   import com.youku.events.PlayerControlEvent;
   import com.youku.events.ConductorEvent;
   import com.youku.conductor.ConductorState;
   import flash.display.DisplayObject;
   import com.youku.PlayerConfig;
   
   public class LoadingScreenMediator extends Object
   {
      
      public function LoadingScreenMediator(param1:MovieClip, param2:IPlayerProxy) {
         super();
         this._mainMc = param1;
         this._playerProxy = param2;
         var _loc3_:DisplayObject = null;
         if(PlayerConfig.loadingMC)
         {
            _loc3_ = PlayerConfig.loadingMC;
         }
         this._loadingScreen = new LoadingScreen(_loc3_);
         this.Init();
      }
      
      private var _mainMc:MovieClip;
      
      private var _playerProxy:IPlayerProxy;
      
      private var _loadingScreen:LoadingScreen;
      
      private function Init() : void {
         this._playerProxy.addEventListener(PlayerControlEvent.SHOW_LOADING,this.onPlayerEvent);
         this._playerProxy.addEventListener(PlayerControlEvent.HIDE_LOADING,this.onPlayerEvent);
         this._playerProxy.addEventListener(PlayerControlEvent.COMMON_ERROR,this.onPlayerEvent);
         this._playerProxy.addEventListener(PlayerControlEvent.COMMON_RESET,this.onPlayerEvent);
         this._playerProxy.addEventListener(PlayerControlEvent.PLAYER_SIZE_CHANGED,this.onPlayerEvent);
         this._playerProxy.addEventListener(PlayerControlEvent.FULLSCREEN_CHANGED,this.onPlayerEvent);
         this._playerProxy.addEventListener(ConductorEvent.STATE_CHANGED,this.onPlayerStateChanged);
         this.loadingScreen.x = 0;
         this.loadingScreen.y = 0;
      }
      
      private function onPlayerStateChanged(param1:ConductorEvent) : void {
         var _loc2_:String = param1.data as String;
         switch(_loc2_)
         {
            case ConductorState.ERROR:
               this.hideLoading();
               break;
         }
      }
      
      private function get loadingScreen() : LoadingScreen {
         return this._loadingScreen;
      }
      
      private function onPlayerEvent(param1:PlayerControlEvent) : void {
         var _loc2_:* = NaN;
         var _loc3_:* = NaN;
         var _loc4_:* = NaN;
         var _loc5_:* = NaN;
         switch(param1.type)
         {
            case PlayerControlEvent.SHOW_LOADING:
               this.showLoading();
               break;
            case PlayerControlEvent.HIDE_LOADING:
               this.hideLoading();
               break;
            case PlayerControlEvent.PLAYER_SIZE_CHANGED:
            case PlayerControlEvent.FULLSCREEN_CHANGED:
               _loc2_ = param1.data.width;
               _loc3_ = param1.data.height;
               _loc4_ = param1.data.fullScreen;
               _loc5_ = param1.data.cheight;
               if(!_loc4_)
               {
                  _loc3_ = _loc3_ - _loc5_;
               }
               this.loadingScreen.setSize(_loc2_,_loc3_);
               break;
            case PlayerControlEvent.COMMON_RESET:
            case PlayerControlEvent.COMMON_ERROR:
               this.hideLoading();
               break;
         }
      }
      
      private function showLoading() : void {
         if(!this._mainMc.contains(this.loadingScreen))
         {
            this._mainMc.addChildAt(this.loadingScreen,0);
         }
         this.loadingScreen.visible = true;
      }
      
      private function hideLoading() : void {
         if(this._mainMc.contains(this.loadingScreen))
         {
            this._mainMc.removeChild(this.loadingScreen);
         }
         this.loadingScreen.visible = false;
      }
   }
}
